package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.text.SimpleDateFormat;
import java.time.Duration;

public class UpdatePage extends Utils {
	public static String url="http://8.148.81.42:8081/blog_list.html";


	public UpdatePage()
	{
		super(url);
	}

	/**
	 * 更新博客成功
	 */
	public void updateSuc() throws InterruptedException {
		//回到列表页
		driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
		//选择一篇博客查看全文内容
		driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
		//点击编辑内容
		driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.edit-btn")).click();
		Thread.sleep(1000);
		//清空标题内容
		driver.findElement(By.cssSelector("#title")).clear();


		//创建的博客标题要随机生成，具备唯一性----方便后面进行精确校验
		SimpleDateFormat sim=new SimpleDateFormat("HHmmssSS");
		String titleTime = sim.format(System.currentTimeMillis());
		String title="自动创建博客"+titleTime;



		//找到标题输入框并输入内容
		driver.findElement(By.cssSelector("#title")).sendKeys(title);


		Thread.sleep(2000);
		//解决：通过原生的键盘操作来处理该元素--Actions
		WebElement ele=driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll"));
		Actions action=new Actions(driver);
		action.doubleClick(ele).sendKeys("哈哈哈哈哈").perform();//双击内容区域

		Thread.sleep(3000);
		//获取更新后的标题
		String updatetitle= driver.findElement(By.cssSelector("#title")).getAttribute("value");
		//获取更新后的博客内容
		String updateblog= driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")).getText();

		//点击更新文章
		driver.findElement(By.cssSelector("#submit")).click();
		Thread.sleep(2000);
		//获取列表页该博客的标题
		String updateAftertitle= driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
		//获取列表页该博客的内容
		String updateAfterblog=driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.desc")).getText();

		//验证更新前后是否一致
		assert updateAftertitle.equals(updatetitle);
		assert updateAfterblog.equals(updateblog);

	}

	/**
	 * 更新博客失败
	 * ----标题为空
	 * ----内容为空
	 * ----标题和内容都为空
	 */
	public void updateFail() throws InterruptedException {
		//选择一篇博客查看全文内容
		driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
		//点击编辑内容
		driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.edit-btn")).click();
		Thread.sleep(1000);

		//标题为空
		//清空标题内容
		driver.findElement(By.cssSelector("#title")).clear();
		//点击更新文章
		driver.findElement(By.cssSelector("#submit")).click();
		//等待弹窗出现
		wait.until(ExpectedConditions.alertIsPresent());
		//处理错误弹窗
		Alert alert = driver.switchTo().alert();
		alert.accept();




		//内容为空
		driver.navigate().refresh();
		WebElement ele=driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll"));
		Actions action=new Actions(driver);
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

		// 先等待元素可见，确保编辑器加载完成
		wait.until(ExpectedConditions.visibilityOf(ele));

		// 双击激活 → 等待选中生效 → 全选→ 删除
		action.doubleClick(ele) // 双击激活并选中内容
				.pause(Duration.ofMillis(500)) // 等待 500ms，确保选中状态生效
				.keyDown(Keys.CONTROL) // 按住 Ctrl 键
				.sendKeys("a") // 按 A 全选
				.keyUp(Keys.CONTROL) // 松开 Ctrl 键
				.sendKeys(Keys.BACK_SPACE) // 按删除键清空
				.perform();

		Thread.sleep(2000);
		//点击更新文章
		driver.findElement(By.cssSelector("#submit")).click();
		//等待弹窗出现
		wait.until(ExpectedConditions.alertIsPresent());
		//选择”确定”----返回当前页
		alert.accept();



	}


}
